<template>
	<view class="home">
		<!-- 自定义导航栏组件 -->
		<navbar></navbar>
		<tab :list="tabList" :tabIndex="tabIndex" @tab="tab"></tab>

		<view class="home-list">
			<list :tab="tabList" :activeIndex="activeIndex" @change="change"></list>
		</view>

	</view>
</template>

<script>
	import { mapState } from 'vuex'
	export default {
		data() {
			return {
				title: 'Hello',
				tabList: [], //导航栏信息
				tabIndex: 0,
				activeIndex: 0
			}
		},
		computed: {
			...mapState(['userinfo'])
		},
		watch: {
			userinfo(newVal) {
				// 获取 tab 导航栏的信息
				this.getLabel();
			}
		},
		onLoad() {

			// 监听标签也的更新操作
			uni.$on('labelChange', (res) => {
				this.tabList = [];
				this.tabIndex = 0;
				this.activeIndex = 0;
				this.getLabel();
			})
		},
		methods: {
			// 监听滚动
			change(current) {
				this.tabIndex = current;
				this.activeIndex = current;
			},

			getLabel() {
				this.$api.get_label().then(res => {
					const {
						data
					} = res;
					data.unshift({
						name: '全部'
					});
					this.tabList = data;
				})
			},
			tab({
				data,
				index
			}) {
				this.activeIndex = index;
			}

		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		display: flex;
	}

	.home {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: hidden;

		.home-list {
			flex: 1;
			box-sizing: border-box;
		}
	}
</style>
